package cn.dw.web.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;

public class ChangetoJson
{
  public String json(List<Map<String, Object>> list)
  {
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    try
    {
      request.setCharacterEncoding("utf-8");
      response.setCharacterEncoding("utf-8");
    }
    catch (UnsupportedEncodingException e)
    {
      e.printStackTrace();
    }
    String json = JSONArray.fromObject(list).toString();
    try
    {
      response.setHeader("Cache-Control", "no-cache");
      response.setContentType("text/javascript;charset=UTF-8");
      response.getWriter().print(json);
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return null;
  }
}
